using MultiAgentsClient.Service;
using MultiAgentsServer.Scripts.Shared;
using MultiAgentsServer.Scripts.Shared.enums;
using MultiAgentsShared;
using static MultiAgentsClient.Manager.NavigationManager;
using static MultiAgentsClient.Manager.UserInfoManager;

namespace MultiAgentsClient.Presentation;

public partial record LoginPageModel
{
    private readonly EventBus eventBus;

    public string VersionNumber;

    public IState<string> InputUsername => State.Value(this, () => "");
    public IState<string> InputPassword => State.Value(this, () => "");
    public IState<bool> IsRemeberMe => State.Value(this, () => false);
    public IListState<string> Servers => ListState<string>.Empty(this);
    public IState<string> SelectedServer => State.Value(this, () => "");
    
    public LoginPageModel()
    {
        eventBus = EventBus.Instance;

        eventBus.Subscribe<LoginCompletedCommand>(cmd => Task.Run(() => LoginResultEvent(cmd)));

        ServerIPConfig serverIPConfig = AppConfigService.GetSection<ServerIPConfig>("ServerIPConfig");
        foreach (string serverName in serverIPConfig.ServerName)
        {
#if !LOCALDEV
            //Skip localDev server if not localDev build
            if (serverName == "LocalDevServer")
            {
                continue;
            }
#endif

            _ = Servers.AddAsync(serverName);
        }
#if LOCALDEV
        SelectedServer.SetAsync(serverIPConfig.ServerName[0]);
#else
        SelectedServer.SetAsync(serverIPConfig.ServerName[2]);
#endif
        string fileContent = FileOperationToolProvider.LoadFile(FileConsts.VersionNumberFileName).Trim();

#if LOCALDEV
        VersionNumber = "version(LocalDev): " + (fileContent.Length > 10 ? "none" : fileContent);
#elif DEVELOP
        VersionNumber = "version(Develop): " + (fileContent.Length > 10 ? "none" : fileContent);
#elif RELEASE
        VersionNumber = "version(Release): " + (fileContent.Length > 10 ? "none" : fileContent);
#endif
    }

    public async ValueTask Login()
    {
        string? username = await InputUsername;
        string? password = await InputPassword;

        if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
        {
            Debug.LogWindow("登录失败，用户名、密码不可为空。");
            return;
        }

        eventBus.Publish(new LoginCommand(username, password, await IsRemeberMe));
    }

    private void LoginResultEvent(LoginCompletedCommand cmd)
    {
        switch (cmd.Status)
        {
            
            case StatusCode.Status200:
                Debug.LogWindow("登录成功。");
                eventBus.Publish(new NavigationCommand(typeof(MainPage)));
                break;
            case StatusCode.Status4002:
                Debug.LogWindow("登录失败，请检查您的用户名和密码后重试。");
                break;
            case StatusCode.Status4003:
                Debug.LogWindow("自动登录失败。");
                break;
            case StatusCode.Status4004:
                Debug.LogWindow("自动登录已过期，请手动登录。");
                break;
            case StatusCode.Status4005:
                Debug.LogWindow("自动登录失败，未知错误，请联系管理员。");
                break;
            default:
                Debug.LogWindow("登录失败，未知错误，请联系管理员。");
                break;
        }
    }

    public void NavigationToRegister()
    {
        eventBus.Publish(new NavigationCommand(typeof(RegisterPage)));
    }
}
